﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySoftWare.BaseModel;
using MySoftWare.DataAccess;
using System.Data.Common;
using System.Data;

namespace MySoftWare.DataCommon
{
    /// <summary>
    /// added by lqm 2013.05.31
    /// 菜单操作相关
    /// </summary>
    public class MenuAccessor
    {
        /// <summary>
        /// 获取菜单列表
        /// 之后须优化 http://www.yongfa365.com/Item/SQL-Di-Gui-Function-ID-ParentID.html
        /// </summary>
        private const string Sql_GetSysMenuList = @"select MenuID,MenuName,MenuParent,MenuDescript,MenuUrl from Sys_Menu";
        public static List<MenuModel> GetSysMenuList()
        {
            List<MenuModel> List = new List<MenuModel>();
            DbDataReader Reader= DBHelper.ExecuteReader(Sql_GetSysMenuList);
            while (Reader.Read())
            {
                MenuModel Menu = new MenuModel();
                Menu.MenuID = Reader.IsDBNull(0) ? -2 : Reader.GetInt32(0);
                Menu.MenuName = Reader.IsDBNull(1) ? "" : Reader.GetString(1);
                Menu.MenuParent = Reader.IsDBNull(2) ? -2 : Reader.GetInt32(2);
                Menu.MenuDescript = Reader.IsDBNull(3) ? "" : Reader.GetString(3);
                Menu.MenuUrl = Reader.IsDBNull(4) ? "" : Reader.GetString(4);
                List.Add(Menu);
            }
            return List;
        }

        /// <summary>
        /// 获取某ID的子菜单还需优化
        /// GetMenuChild为一个函数
        /// </summary>
        private const string Sql_GetMenuChild = @"select * from GetMenuChild(@MenuID)";
        public static List<MenuModel> GetMenuChild(string MenuID)
        {
            List<MenuModel> List = new List<MenuModel>();
            DbParameter[] Params = { 
                                        DBHelper.MakeInParam("@MenuID", DbType.Int32,MenuID)
                                   };
            DbDataReader Reader = DBHelper.ExecuteReader(CommandType.Text, Sql_GetMenuChild, Params);
            while (Reader.Read())
            {
                MenuModel Menu = new MenuModel();
                Menu.MenuID = Reader.IsDBNull(0) ? -2 : Reader.GetInt32(0);
                Menu.MenuName = Reader.IsDBNull(1) ? "" : Reader.GetString(1);
                Menu.MenuParent = Reader.IsDBNull(2) ? -2 : Reader.GetInt32(2);
                Menu.MenuUrl = Reader.IsDBNull(3) ? "" : Reader.GetString(3);
                Menu.Level = Reader.IsDBNull(4) ? -1 : Reader.GetInt32(4);
                List.Add(Menu);
            }
            return List;
        }

        /// <summary>
        /// Tree的列表包含子节点暂时考虑使用递归的方式获取 added by lqm 2013.06.13  之后进行优化
        /// </summary>
        /// <returns></returns>
        private const string Sql_GetTreeMenuList = @"select MenuID,MenuName,MenuParent,MenuDescript,MenuUrl from  Sys_Menu where MenuParent=@MenuID";
        public static List<TreeMenuModel> GetTreeMenuList(int ParentID=-1)
        {
            List<TreeMenuModel> List = new List<TreeMenuModel>();
            DbParameter[] Params = { 
                                        DBHelper.MakeInParam("@MenuID", DbType.Int32,ParentID)
                                   };
            DbDataReader Reader = DBHelper.ExecuteReader(CommandType.Text, Sql_GetTreeMenuList, Params);
            while (Reader.Read())
            {
                TreeMenuModel tree = new TreeMenuModel();
                {
                    tree.MenuID = Reader.GetInt32(0);
                    tree.MenuName = Reader.GetString(1);
                    tree.MenuParent = Reader.GetInt32(2);
                    tree.MenuDescript = Reader.IsDBNull(3) ? "" : Reader.GetString(3);
                    tree.MenuUrl = Reader.IsDBNull(4) ? "" : Reader.GetString(4);
                    tree.Children = GetTreeMenuList(tree.MenuID);
                }
                List.Add(tree);
            }
            return List;
        }
    }
}
